package imusic.baidu;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import java.net.URL;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Top200Singer {
    static String savaPath = "/home/h/BaiduTop200Singers.txt";
    public static Queue<Singer> sinerQueue = new ConcurrentLinkedQueue<Singer>();


    public static void add() throws Exception {

        HtmlCleaner cleaner = new HtmlCleaner();
        TagNode tagNode = cleaner.clean(new URL(
                "http://music.baidu.com/top/artist"), "utf8");
        Object[] singerName1 = tagNode
                .evaluateXPath("//div[@class='artist-name']/text()");
        Object[] id1 = tagNode
                .evaluateXPath("//div[@class='artist-name']/a");

        Object[] singerName2 = tagNode
                .evaluateXPath("//span[@class='artist-name']/text()");
        Object[] id2 = tagNode
                .evaluateXPath("//span[@class='artist-name']/a");

        int length1 = singerName1.length;
        for (int i = 0; i < length1; i++) {
            Singer singer1 = new Singer();
            singer1.setName(singerName1[i].toString().trim());
            singer1.setId(((TagNode) id1[i]).getAttributeByName("href").toString().trim().substring(8));
            sinerQueue.add(singer1);
        }

        int length2 = singerName2.length;
        for (int i = 0; i < length2; i++) {
            Singer singer2 = new Singer();
            singer2.setName(singerName2[i].toString().trim());
            singer2.setId(((TagNode) id2[i]).getAttributeByName("href").toString().trim().substring(8));
            sinerQueue.add(singer2);
        }
    }


    public static void sava() throws Exception {
        add();
        StringBuffer buffer = new StringBuffer();
        int i = 0;
        for (Singer singer : sinerQueue) {
            buffer.append(++i);
            buffer.append("|");
            buffer.append(singer.getName());
            buffer.append("\n");
        }
        Util.writeFile(buffer, savaPath);
    }


    public static void main(String[] args) throws  Exception{
        Top200Singer.sava();
        SongsOfTop200Singer.sava();
    }

}
